#ifndef __ACCEPTOR_H__
#define __ACCEPTOR_H__
#include "Socket.h"
#include "InetAddr.h"
#include "EventLoop.h"

class Acceptor
{
public:
    using TcpConnectionPtr = shared_ptr<TcpConnection>;
    Acceptor(EventLoop *loop, const string &ip, unsigned short port);
    ~Acceptor();
    void ready();//执行地址端口复用\bind\listen
    int accept();//处理新连接
    bool isListening() const { return _isListening; }
    void setNewConnectionCallback(const function<void(int sockfd, int connfd)> &cb)
    {
        _newConnectionCallback = cb;
    }

    int fd();
    void listen();
    void handleRead(); // 处理新连接
private:
    void setReuseAddr();
    void setReusePort();
    void bind();


private:
    EventLoop *_loop; //所属事件循环
    Socket _sock;
    InetAddr _addr;
    bool _isListening; //是否在监听
    function<void(int sockfd, int connfd)> _newConnectionCallback; //新连接回调
};
#endif
